//
//  ShellWithdrawView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellWithdrawView.h"
#import "ShellWithdrawViewModel.h"
#import "ShellWithdrawHeadView.h"
#import "ShellWithdrawRecordCell.h"
#import "LZBaseTableView.h"



@interface ShellWithdrawView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellWithdrawViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;

@property (nonatomic,strong) UIButton *erc20Btn;
@property (nonatomic,strong) UIButton *omniBtn;



@end

@implementation ShellWithdrawView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellWithdrawViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    CGFloat tableViewY = 76;

    switch (self.viewModel.wallertModel.currencyType) {
        case ShellCurrencyTypeETH_USDT:{
            [self addSubview:self.erc20Btn];
            [self.erc20Btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(15);
                make.left.mas_equalTo(28);
                make.width.mas_equalTo((K_SCREENWIDTH - 56 - 16)/2.0);
                make.height.mas_equalTo(42);
            }];
            
            
            [self addSubview:self.omniBtn];
            [self.omniBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(15);
                make.right.mas_equalTo(-28);
                make.width.mas_equalTo((K_SCREENWIDTH - 56 - 16)/2.0);
                make.height.mas_equalTo(42);
            }];

        }
            break;
        case ShellCurrencyTypeETH:{
            tableViewY = 0;
        }
            break;
        default:
            tableViewY = 0;
            break;
    }

        
    ShellWithdrawHeadView *headView = [[ShellWithdrawHeadView alloc] initViewModel:self.viewModel];
    headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 445);
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tableViewY);
        make.left.right.bottom.mas_equalTo(0);
    }];
    self.tableView.tableHeaderView = headView;

    
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self reloadRecordData:[x integerValue]];
    }];
    
    
    [self.viewModel getCoinRecord:YES];
    
    [self.tableView addAutoNormalFooterWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getCoinRecord:NO];
    }];
    
    //提币限额
    [self.viewModel getWithdrawLimit];

}

- (void)reloadRecordData:(NSInteger)type{
    
    if (type == 4) {
        return;
    }
    
    
    self.tableView.mj_header.state  = MJRefreshStateIdle;
    self.tableView.mj_footer.state  = MJRefreshStateIdle;
    [self.tableView reloadData];
    
    switch (type) {
        case 0:{
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
            break;
        default:
            break;
    }
 
}

- (void)coinSwitchClick:(UIButton *)sender{
    sender.backgroundColor = UIColorHex(0x4AB2E7);
    if (sender == self.erc20Btn) {
        self.omniBtn.backgroundColor = UIColorHex(0x283040);
        self.viewModel.wallertModel.currencyType = ShellCurrencyTypeETH_USDT;
    } else {
        self.erc20Btn.backgroundColor = UIColorHex(0x283040);
        self.viewModel.wallertModel.currencyType = ShellCurrencyTypeUSDT;
    }
    [self.viewModel.reloadSubject sendNext:@(4)];

}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShellWithdrawRecordCell *recordCell = [tableView dequeueReusableCellWithIdentifier:@"ShellWithdrawRecordCell"];
    recordCell.indexPath = indexPath;
    if(self.viewModel.dataArray.count > indexPath.row){
        recordCell.modelObject = self.viewModel.dataArray[indexPath.row];
    }
    return recordCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = COLOR_BACKGROUND;
    sectionView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 50);
    //数量
    UILabel *numberLab = [UILabel lz_labelWithText:LZLocalizedString(@"数量") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
    numberLab.frame = CGRectMake(15, 15, 60, 20);
    [sectionView addSubview:numberLab];
    
    //状态
    UILabel *statusLabel = [UILabel lz_labelWithText:LZLocalizedString(@"状态") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
    statusLabel.frame = CGRectMake(100, 15, 90, 20);
    [sectionView addSubview:statusLabel];
    
    //时间
      UILabel *timeLabel = [UILabel lz_labelWithText:LZLocalizedString(@"时间") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
      timeLabel.frame = CGRectMake(200, 15, K_SCREENWIDTH - 215 - 15 - 60, 20);
      [timeLabel lz_centerAlignment];
      [sectionView addSubview:timeLabel];
      
      
      //连接
       UILabel *linkLabel = [UILabel lz_labelWithText:LZLocalizedString(@"详情") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
       linkLabel.frame = CGRectMake(K_SCREENWIDTH - 15 - 60, 15, 50, 20);
       [linkLabel lz_centerAlignment];
       [sectionView addSubview:linkLabel];
    return sectionView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}


#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellWithdrawRecordCell class] forCellReuseIdentifier:@"ShellWithdrawRecordCell"];
        _tableView.rowHeight = 50;
    }
    return _tableView;
}


- (UIButton *)erc20Btn{
    if (!_erc20Btn) {
        _erc20Btn = [UIButton lz_buttonTitle:@"ERC20" titleColor:UIColorHex(0xFFFFFF) fontSize:16];
        [_erc20Btn addTarget:self action:@selector(coinSwitchClick:) forControlEvents:UIControlEventTouchUpInside];
        _erc20Btn.cornerRadius = 4;
        _erc20Btn.backgroundColor = UIColorHex(0x4AB2E7);

    }
    return _erc20Btn;
}

- (UIButton *)omniBtn{
    if (!_omniBtn) {
        _omniBtn = [UIButton lz_buttonTitle:@"OMNI" titleColor:UIColorHex(0xFFFFFF) fontSize:16];
        [_omniBtn addTarget:self action:@selector(coinSwitchClick:) forControlEvents:UIControlEventTouchUpInside];
        _omniBtn.cornerRadius = 4;
        _omniBtn.backgroundColor = UIColorHex(0x283040);

    }
    return _omniBtn;
}
@end
